package com.wenhui.schoolyardinfo.message.adapter;

import android.content.Context;
import android.support.annotation.NonNull;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.FrameLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.wenhui.schoolyardinfo.R;
import com.wenhui.schoolyardinfo.callback.RcvOnItemViewClickListener;
import com.wenhui.schoolyardinfo.callback.RcvOnLongItemTripletsViewClickListener;
import com.wenhui.schoolyardinfo.customview.CircleImageView;
import com.wenhui.schoolyardinfo.manager.DateManager;
import com.wenhui.schoolyardinfo.message.bean.NoticeBean;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;

/**
 * author    : Urasaki
 * e-mail    : 1164688204@qq.com
 * date      : 2019/6/28 15:49
 * introduce : 通知Adapter
 */

public class NoticeAdapter extends RecyclerView.Adapter {

    private Context context;
    private List<NoticeBean> noticeBeanList = new ArrayList<>();

    public NoticeAdapter(Context context) {
        this.context = context;
    }

    public NoticeAdapter(Context context, List<NoticeBean> noticeBeanList) {
        this.context = context;
        this.noticeBeanList = noticeBeanList;
    }

    public void clearData() {
        noticeBeanList.clear();
        notifyDataSetChanged();
    }

    public void addAllData(List<NoticeBean> noticeBeanList) {
        this.noticeBeanList.addAll(noticeBeanList);
        notifyDataSetChanged();
    }

    @NonNull
    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(context).inflate(R.layout.item_notice, parent, false);
        return new ContentHolder(view);
    }

    @Override
    public void onBindViewHolder(@NonNull RecyclerView.ViewHolder holder, int position) {
        if (holder instanceof ContentHolder) {
            ContentHolder contentHolder = (ContentHolder) holder;
            Glide.with(context).load(noticeBeanList.get(position).getHeadPortraitUrl()).into(contentHolder.imvHeadPortrait);
            contentHolder.tevName.setText(noticeBeanList.get(position).getName());
            contentHolder.tevTime.setText(DateManager.millisecondConvertedToDate(noticeBeanList.get(position).getTime()));
            contentHolder.tevNoticeMessage.setText(noticeBeanList.get(position).getNoticeMessage());
            if (!noticeBeanList.get(position).isSystemAnnouncement()) {//如果不是系统消息
                contentHolder.layoutRight.setVisibility(View.VISIBLE);
                if (noticeBeanList.get(position).isAttentionCan()) {//如果可以关注
                    contentHolder.layoutConcernAdd.setVisibility(View.VISIBLE);
                    contentHolder.tevReply.setVisibility(View.GONE);
                } else {//如果不可以关注
                    contentHolder.layoutConcernAdd.setVisibility(View.GONE);
                    contentHolder.tevReply.setVisibility(View.VISIBLE);
                }
            } else {//如果是系统消息
                contentHolder.layoutRight.setVisibility(View.GONE);
            }
        }
    }

    @Override
    public int getItemCount() {
        return noticeBeanList.size();
    }

    protected class ContentHolder extends RecyclerView.ViewHolder implements View.OnClickListener, View.OnLongClickListener {

        @BindView(R.id.imv_head_portrait)
        CircleImageView imvHeadPortrait;
        @BindView(R.id.tev_name)
        TextView tevName;
        @BindView(R.id.tev_notice_message)
        TextView tevNoticeMessage;
        @BindView(R.id.tev_time)
        TextView tevTime;
        @BindView(R.id.tev_concern_add)
        TextView tevConcernAdd;
        @BindView(R.id.layout_concern_add)
        RelativeLayout layoutConcernAdd;
        @BindView(R.id.tev_reply)
        TextView tevReply;
        @BindView(R.id.layout_right)
        FrameLayout layoutRight;
        @BindView(R.id.view_notice)
        View viewNotice;
        @BindView(R.id.layout_notice)
        RelativeLayout layoutNotice;

        private ContentHolder(View itemView) {
            super(itemView);
            ButterKnife.bind(this, itemView);

            imvHeadPortrait.setOnClickListener(this);
            tevConcernAdd.setOnClickListener(this);
            layoutNotice.setOnClickListener(this);
            layoutNotice.setOnLongClickListener(this);
        }

        @Override
        public void onClick(View view) {
            switch (view.getId()) {
                case R.id.imv_head_portrait:
                    rcvOnItemViewClickListener.onItemClickListener(getAdapterPosition(), view);
                    break;
                case R.id.tev_concern_add:
                    rcvOnItemViewClickListener.onItemClickListener(getAdapterPosition(), view);
                    break;
                case R.id.layout_notice:
                    rcvOnItemViewClickListener.onItemClickListener(getAdapterPosition(), view);
                    break;
                default:
                    break;
            }
        }

        @Override
        public boolean onLongClick(View view) {
            switch (view.getId()) {
                case R.id.layout_notice:
                    rcvOnLongItemTripletsViewClickListener.onLongItemClickListener(getAdapterPosition(), view, tevName, tevNoticeMessage);
                    break;
                default:
                    break;
            }
            return true;
        }
    }

    private RcvOnItemViewClickListener rcvOnItemViewClickListener;

    public void setRcvOnItemViewClickListener(RcvOnItemViewClickListener rcvOnItemViewClickListener) {
        this.rcvOnItemViewClickListener = rcvOnItemViewClickListener;
    }

    private RcvOnLongItemTripletsViewClickListener rcvOnLongItemTripletsViewClickListener;

    public void setRcvOnLongItemTripletsViewClickListener(RcvOnLongItemTripletsViewClickListener rcvOnLongItemTripletsViewClickListener) {
        this.rcvOnLongItemTripletsViewClickListener = rcvOnLongItemTripletsViewClickListener;
    }
}
